package com.ray.project.oceanicwaveinformation;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.ray.project.oceanicbuoy.OceanicBuoy;
import com.ray.project.oceanicwaveinformation.R;

import java.util.List;


public class OceanicBuoyItemizedOverlay extends ItemizedOverlay<OceanicBuoyOverlayItem> {

    private final List<OceanicBuoyOverlayItem> oceanicBuoyOverlayItems;
    private final Context currentApplicationContext;

    public OceanicBuoyItemizedOverlay(final List<OceanicBuoyOverlayItem> items, final Drawable defaultMarker, final Context context) {
        super(defaultMarker);
        this.oceanicBuoyOverlayItems = items;
        this.currentApplicationContext = context;
        populate();
    }

    @Override
    public OceanicBuoyOverlayItem createItem(final int i) {
        return this.oceanicBuoyOverlayItems.get(i);
    }
   
    @Override
    protected boolean onTap(final int i) {
        final OceanicBuoy buoyData = this.oceanicBuoyOverlayItems.get(i).oceanicBuoyData;
   
        LayoutInflater inflater = LayoutInflater.from(this.currentApplicationContext);
        View bView = inflater.inflate(R.layout.oceanic_buoy_selected, null);
        TextView buoyTitle = (TextView) bView.findViewById(R.id.buoy_title);
        buoyTitle.setText(buoyData.getTitle());
        TextView location = (TextView) bView.findViewById(R.id.buoy_location);
        location.setText(buoyData.getLocation());

        String waveHeight = "Not Available";
        if (buoyData.getWaveHeight() != null) {
            waveHeight = buoyData.getWaveHeight();
        }

        TextView whView = (TextView) bView.findViewById(R.id.waveheight);
        whView.setText("Wave height: " + waveHeight);

        new AlertDialog.Builder(this.currentApplicationContext).setView(bView).setPositiveButton("More Info...",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface di, int what) {
                    Intent intent = new Intent(currentApplicationContext, OceanicBuoyDetailActivity.class);
                    OceanicBuoyDetailActivity.buoyData = buoyData;
                    currentApplicationContext.startActivity(intent);
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface di, int arg) {
                di.dismiss();
            }
        }).show();

        return true;
    }

    @Override
    public int size() {
        return this.oceanicBuoyOverlayItems.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean b) {
        super.draw(canvas, mapView, false);          
    }
}
